package cn.zch.soft.util.hutool.http;
import javax.xml.namespace.QName;
import javax.xml.soap.*;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import java.net.URL;
public class WsUtilsTest {
  /**
   * 用java自带的api调用相应的webservice接口
   *
   */
  public static void main(String[] args) {
    originalTest1();
  }
  public static void originalTest1() {
    try {
      // http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl
      // http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName
      URL url = new URL("http://www.webxml.com.cn/WebServices/WeatherWebService.asmx");
      //    接口名称 javax.xml.ws.WebServiceException: {http://tempuri.org/}EmrService不是有效服务。有效服务为: {http://WebXml.com.cn/}WeatherWebService
      QName sname = new QName("http://WebXml.com.cn/", "WeatherWebService");
      Service service = Service.create(url, sname);
      // javax.xml.ws.WebServiceException: {http://tempuri.org/}EmrServiceSoap 不是有效端口。
      // 有效端口为:
      // {http://WebXml.com.cn/}WeatherWebServiceHttpGet,
      // {http://WebXml.com.cn/}WeatherWebServiceSoap12,
      // {http://WebXml.com.cn/}WeatherWebServiceHttpPost,
      // {http://WebXml.com.cn/}WeatherWebServiceSoap
      Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName("http://WebXml.com.cn/", "WeatherWebServiceSoap"), SOAPMessage.class, Service.Mode.MESSAGE);
      //这句话很重要，否则报错服务器未能识别 HTTP 头 SOAPAction 的值
      dispatch.getRequestContext().put("javax.xml.ws.soap.http.soapaction.uri", "http://WebXml.com.cn/getWeatherbyCityName");
      SOAPMessage msg = MessageFactory.newInstance().createMessage();
      SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
      SOAPBody body = envelope.getBody();
      //得到要调用的方法
      QName ename = new QName("http://WebXml.com.cn/", "getWeatherbyCityName");
      SOAPBodyElement ele = body.addBodyElement(ename);
      QName ename1 = new QName("http://WebXml.com.cn/", "theCityName");
      ele.addChildElement(ename1).setValue("郑州");
      SOAPMessage response = dispatch.invoke(msg);
      System.out.println("response = " + response);
      //      SOAPBody soapBody = response.getSOAPPart().getEnvelope().getBody();
      //      soapBody.getAttributeNode("HelloWorldResult");
      //      System.out.println("ffff===" + soapBody.getElementsByTagName("HelloWorldResult").item(0).getTextContent());
      //  org.w3c.dom.Document doc = response.getSOAPPart().getEnvelope().getBody().extractContentAsDocument();
      //这里输入的"loginResult"就是Web服务端定义的login()方法的返回值名称
      //  System.out.println("\n收到响应:" + doc.getElementsByTagName("HelloWorldResult").item(0).getTextContent());
      //  String result = String.format("%1$20s", "abc");
      //  System.out.println(result);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
  /**
   * https://blog.csdn.net/mengfei86/article/details/9852013
   */
  public static void originalTest() {
    try {
      URL url = new URL("http://192.168.11.98/TEST/Service.asmx?wsdl");
      //    接口名称
      QName sname = new QName("http://tempuri.org/", "EmrService");
      Service service = Service.create(url, sname);
      Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName("http://tempuri.org/", "EmrServiceSoap"), SOAPMessage.class, Service.Mode.MESSAGE);
      //这句话很重要，否则报错服务器未能识别 HTTP 头 SOAPAction 的值
      dispatch.getRequestContext().put("javax.xml.ws.soap.http.soapaction.uri", "http://tempuri.org/HelloWorld");
      SOAPMessage msg = MessageFactory.newInstance().createMessage();
      SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
      SOAPBody body = envelope.getBody();
      //得到要调用的方法
      QName ename = new QName("http://tempuri.org/", "HelloWorld");
      SOAPBodyElement ele = body.addBodyElement(ename);
      QName ename1 = new QName("http://tempuri.org/", "strPatientId");
      ele.addChildElement(ename1).setValue("好人");
      //    msg.writeTo(System.out);
      //  System.out.println("");
      SOAPMessage response = dispatch.invoke(msg);
      //  response.writeTo(System.out);
      SOAPBody soapBody = response.getSOAPPart().getEnvelope().getBody();
      soapBody.getAttributeNode("HelloWorldResult");
      System.out.println("ffff===" + soapBody.getElementsByTagName("HelloWorldResult").item(0).getTextContent());
      //  org.w3c.dom.Document doc = response.getSOAPPart().getEnvelope().getBody().extractContentAsDocument();
      //这里输入的"loginResult"就是Web服务端定义的login()方法的返回值名称
      //  System.out.println("\n收到响应:" + doc.getElementsByTagName("HelloWorldResult").item(0).getTextContent());
      //  String result = String.format("%1$20s", "abc");
      //  System.out.println(result);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
  }
}